define(['underscore','backbone','jquery','views/widget','text!templates/text.html','models/richtext'], function(_,Backbone,$,ViewWidget,TextTemplate, RichTextModel){
    var RichTextView = ViewWidget.extend({
        template : _.template(TextTemplate),
        initialize : function(){
            this.model.bind('change', this.render, this);
            this.model.view = this;
        },
        render: function() {
            $(this.el).html(this.template(this.model.toJSON()));
            this.edit();
            return this;
        },
        edit : function()
        {
            tinyMCE.init({
                // General options
                mode : "exact",
                elements : "widget-content-"+this.model.get('id'),
                theme : "advanced",
                plugins : "autolink,lists,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,wordcount,advlist",
                entity_encoding : "numeric",

                // Theme options
                theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,styleselect,formatselect,fontselect,fontsizeselect",
                theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,code,|,forecolor,backcolor",
                theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,ltr,rtl,|,fullscreen",
                theme_advanced_toolbar_location : "top",
                theme_advanced_toolbar_align : "left",
                theme_advanced_statusbar_location : "bottom",
                theme_advanced_resizing : true,
                file_browser_callback : function(field_name, url, type, win) {
                    aFieldName = field_name, aWin = win;

                    if($("#finder").length == 0) {
                        $("body").append($("<div/>").attr("id", "finder"));
                        $("#finder").elfinder({
                            url : $('.connection_url').attr('href'),
                            lang : 'en',
                            dialog : { width : 900, modal : true, title : "File manager", zIndex: 7777777 },
                            editorCallback : $.proxy(function(url) {
                                aWin.document.forms[0].elements[aFieldName].value = url;
                        
                                if (aFieldName=="src" && aWin.ImageDialog.showPreviewImage)
                                    aWin.ImageDialog.showPreviewImage(url);
                            },this),
                            closeOnEditorCallback: true
                        });
                    }
                    else {
                        $("#finder").elfinder("open");
                    }
                }
            });
            ViewWidget.prototype.edit.call(this);
            return false;
        },
        save : function()
        {
            Backbone.emulateJSON = true;
            var exported = this.$('.export input:checkbox').attr('checked') ? 1 : 0;
            this.model.save({content:tinyMCE.activeEditor.getContent(),exported : exported});
            return false;
        }
    });
    return RichTextView;
});